package com.android.delivery.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.delivery.R;
import com.android.delivery.entity.Version;
import com.android.delivery.util.ToastUtils;

import butterknife.BindView;

public class UpdateVersionDialog extends BaseDialog implements View.OnClickListener {

    private static final String EXTRA_VERSION = "version";

    @BindView(R.id.tv_update_version)
    TextView mTvVersion;
    @BindView(R.id.tv_update_content)
    TextView mTvContent;
    @BindView(R.id.tv_dialog_left)
    TextView mTvDialogLeft;
    @BindView(R.id.tv_dialog_right)
    TextView mTvDialogRight;

    private Context mContext;
    private Version mVersion; // 版本号

    public static UpdateVersionDialog newInstance(Context context, Version version) {
        UpdateVersionDialog dialog = new UpdateVersionDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelable(EXTRA_VERSION, version);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.dialog_version_update;
    }

    @Override
    protected void initView(View view) {
        mTvDialogLeft.setVisibility(View.GONE);
        if (getArguments() != null) {
            mVersion = getArguments().getParcelable(EXTRA_VERSION);
            mTvDialogRight.setText(R.string.update_now);
            mTvDialogRight.setOnClickListener(this);
            if (!TextUtils.isEmpty(mVersion.versionName)) mTvVersion.setText("版本：" + mVersion.versionName);
            if (!TextUtils.isEmpty(mVersion.updateContent)) mTvContent.setText(mVersion.updateContent);
            setCancelable(!mVersion.forceUpdate);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_dialog_right:
                downLoadApk();
                dismiss();
                break;
            default:
                break;
        }
    }

    private void downLoadApk() {
        if (mVersion.androidDownloadUrl.startsWith("http:") || mVersion.androidDownloadUrl.startsWith("https:")) {
            ToastUtils.showShort(getActivity(), "开始在后台下载");
            Intent intent = new Intent(getContext(), DownloadService.class);
            intent.putExtra("title", "正在下载" + getContext().getString(R.string.app_name));
            intent.putExtra("url", mVersion.androidDownloadUrl);
            intent.putExtra("path", findDownLoadDirectory());
            intent.putExtra("name", "Mimi" + System.currentTimeMillis() + ".apk");
            getContext().startService(intent);
        } else {
            ToastUtils.showShort(getActivity(), "后台返回链接有误");
        }
    }

    private String findDownLoadDirectory() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        } else {
            return Environment.getRootDirectory() + "/" + "download/";
        }
    }

}
